/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ActionBar;

import CRUD.BranchEnterpriseFacadeLocal;
import CRUD.EnterpriseFacadeLocal;
import CRUD.ItemCampaignFacadeLocal;
import CRUD.ProductFacadeLocal;
import Entity.Addf;
import Entity.BranchEnterprise;
import Entity.Marker;
import Entity.PersonalInfo;
import Entity.Product;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author WinKiQue58
 */
@Stateless
public class SearchOptions implements SearchOptionsLocal {
    
    @PersistenceContext(unitName = "S3R4_Orcl-ejbPU")
        private EntityManager em;
   
    @EJB  private ItemCampaignFacadeLocal itemCampaignFacade;
    @EJB  private ProductFacadeLocal productFacade;
    @EJB  private BranchEnterpriseFacadeLocal branchEnterpriseFacade;
    @EJB  private EnterpriseFacadeLocal enterpriseFacade;
    
    public List<BranchEnterprise> getBranchEnterprises (BigInteger idEnterprise)
    {
        List<BranchEnterprise> list;
        Query q1= em.createQuery("SELECT be FROM BranchEnterprise be WHERE "
                  + "be.branchEnterprisePK.idEnterprise = :A");
        q1.setParameter("A", idEnterprise);
        try{
        list=q1.getResultList();
        }catch(Exception e)
        {
            return null;
        }
        return list;
    }
    
    public List<Product> getAllProductByBranch(BigInteger idBranchEnterprise,BigInteger idEnterprise)
    {
        List<Product> list;
        Query q1= em.createQuery("SELECT pro FROM Product pro WHERE "
                  + "pro.productPK.idEnterprise = :A AND "
                  + "pro.productPK.idBranchenterprise = :B");
        q1.setParameter("A", idEnterprise);
        q1.setParameter("B", idBranchEnterprise);
        try{
        list=q1.getResultList();
        }catch(Exception e)
        {
            return null;
        }
        return list;
    }
    
     public List<Product> getAllProduct(BigInteger idEnterprise)
    {
        List<Product> list;
        Query q1= em.createQuery("SELECT pro FROM Product pro WHERE "
                  + "pro.productPK.idEnterprise = :A");
        q1.setParameter("A", idEnterprise);
        try{
        list=q1.getResultList();
        }catch(Exception e)
        {
            return null;
        }
        return list;
    }
    
    public List<Marker> getMarkers( BigInteger idPersonalInfo, BigInteger idBranchEnterprise, BigInteger idEnterprise)
    {
        List<Marker> list;
        Query q1= em.createQuery("SELECT mkr FROM Marker mkr WHERE "
                  + "mkr.markerPK.idPersonalInfo = :A AND "
                  + "mkr.markerPK.idBranchenterprise = :B AND "
                  + "mkr.markerPK.idEnterprise = :C");
          q1.setParameter("A", idPersonalInfo);
          q1.setParameter("B", idBranchEnterprise);
          q1.setParameter("C", idEnterprise);
        try{
        list=q1.getResultList();
        }catch(Exception e)
        {
            return null;
        }
        return list;
    } 
    
    public List<PersonalInfo> getFriends( BigInteger idPersonalInfo, BigInteger idBranchEnterprise, BigInteger idEnterprise)
    {
        List<Addf> list;
        Query q1= em.createQuery("SELECT a FROM Addf a WHERE "
                  + "a.addfPK.idPersonalInfo = :A AND "
                  + "a.addfPK.idBranchenterprise = :B AND "
                  + "a.addfPK.idEnterprise = :C");
          q1.setParameter("A", idPersonalInfo);
          q1.setParameter("B", idBranchEnterprise);
          q1.setParameter("C", idEnterprise);
        try{
        list=q1.getResultList();
        }catch(Exception e)
        {
            return null;
        }
        
        List<PersonalInfo> list2 = new ArrayList<PersonalInfo> ();
        for (Addf addf : list) {
             Query q2= em.createQuery("SELECT pi FROM PersonalInfo pi WHERE "
                  + "a.addfPK.idPersonalInfo = :A AND "
                  + "a.addfPK.idBranchenterprise = :B AND "
                  + "a.addfPK.idEnterprise = :C");
             q2.setParameter("A", addf.getPersonalInfo1().getPersonalInfoPK().getIdPersonalInfo());
             q2.setParameter("B", addf.getPersonalInfo1().getPersonalInfoPK().getIdBranchenterprise());
             q2.setParameter("C", addf.getPersonalInfo1().getPersonalInfoPK().getIdEnterprise());
             
                     try{
                     list2.add( (PersonalInfo)q2.getSingleResult());
                    }catch(Exception e)
                    {
                        return null;
                    }
            
        }
        
        
        return list2;
    } 
}
